package com.ucap.weking.guangan.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ucap.weking.guangan.Activitys.NewContentActivity;
import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.adapter.LIstAdapter;
import com.ucap.weking.guangan.base.BaseDialog;
import com.ucap.weking.guangan.bean.ListItemBean;
import com.ucap.weking.guangan.httpTools.HttpCallRequest;
import com.ucap.weking.guangan.modle.ItemModel;
import com.ucap.weking.guangan.views.LoadingListView;
import com.ucap.weking.guangan.views.MyToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by weking on 2017/10/25.
 */

public class ListDialog extends BaseDialog {


    @InjectView(R.id.lv_content)
    LoadingListView lvContent;
    @InjectView(R.id.pb_loading)
    ProgressBar pbLoading;
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.iv_dismiss)
    ImageView ivDismiss;
    String contentUri;
    Activity activity;
    String title;
    private LIstAdapter adapter;
    private int numow = 1;//页面开始角标
    private int num = 10;//页面结束角标

    protected ListDialog(Context context, Activity activity, String title, String contentUri) {
        super(context);
        this.contentUri = contentUri;
        this.activity = activity;
        this.title = title;
    }

    public static void showDialog(Context ctx, Activity activity, String title, String contentUri) {
        ListDialog dialog = new ListDialog(ctx, activity, title, contentUri);
        dialog.show();
    }

    @Override
    public void setView() {
        setContentView(R.layout.dialog_list);
        ButterKnife.inject(this);
    }

    @Override
    public void setListener() {
        ivDismiss.setOnClickListener(this);
    }

    @Override
    public void setData() {
        numow = 1;
        num = 10;
        final ArrayList<ItemModel> mData = new ArrayList<>();
        adapter = new LIstAdapter(mData);
        lvContent.setAdapter(adapter);
        getData(mData, numow, num);
        tvTitle.setText(title);
        lvContent.setInterface(new LoadingListView.IloadListener() {
            @Override
            public void onLoad() {
                getData(mData, numow += 10, num += 10);
            }
        });
        lvContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mData.size() > 0 && position > -1 && position < mData.size())
                    if (adapter.getItem(position).isClick())
                        startContentActivity(adapter.getItem(position).getContentUri());
            }
        });
    }

    private void getData(final ArrayList<ItemModel> mData, final int startNum, final int endNum) {
        if (startNum == 1) {
            pbLoading.setVisibility(View.VISIBLE);
            lvContent.setVisibility(View.INVISIBLE);
        }
        HttpCallRequest.callRequest(contentUri + "numtwo=" + startNum + "&num=" + endNum, new HttpCallRequest.OnCallListener() {
            @Override
            public void onRequest(final String body) {
                Log.i("companyData", "onRequest: " + body);
                Log.i("companyData", "uri: " + contentUri + "numtwo=" + startNum + "&num=" + endNum);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Gson gson = new Gson();
                            ListItemBean listItemBean = gson.fromJson(body, ListItemBean.class);
                            if (listItemBean != null) {
                                ArrayList<ItemModel> data = new ArrayList<>();
                                List<ListItemBean.RowsBean> rows2 = listItemBean.getRows();
                                if (rows2 != null && rows2.size() > 0) {
                                    for (ListItemBean.RowsBean bean2 : rows2) {
                                        ItemModel model = new ItemModel(bean2.getTITLE(), bean2.getTIME(), bean2.getID());
                                        model.setImageId(bean2.getPATHANDURL());
                                        model.setContentUri(bean2.getURL());
                                        model.setTimer(false);
                                        if (title.equals("区市县人民政府"))
                                            model.setClick(false);
                                        else
                                            model.setClick(true);
                                        data.add(model);
                                    }
                                    mData.addAll(data);
                                    adapter.notifyDataSetChanged();
                                    lvContent.loadComplete();
                                } else {
                                    MyToast.showTrue(getContext(), "已加载全部！");
                                    lvContent.loadComplete();
                                    if (numow > 1) {
                                        numow -= 10;
                                        num -= 10;
                                    }
                                }
                            } else {
                                MyToast.showTrue(getContext(), "已加载全部！");
                                lvContent.loadComplete();
                                if (numow > 1) {
                                    numow -= 10;
                                    num -= 10;
                                }
                            }
                            pbLoading.setVisibility(View.GONE);
                            lvContent.setVisibility(View.VISIBLE);
                        } catch (Exception e) {
                            dismiss();
                        }
                    }
                });
            }

            @Override
            public void onErr(Exception e) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        lvContent.loadComplete();
                        adapter.notifyDataSetChanged();
                        if (numow > 1) {
                            numow -= 10;
                            num -= 10;
                        }
                        MyToast.showTrue(getContext(), "连接超时，请检查网路连接！");
                    }
                });

            }
        });
    }


    @Override
    public void setClick(View v) {
        if (v == ivDismiss) {
            dismiss();
        }
    }

    public void startContentActivity(String uri) {
        Intent intent = new Intent(activity, NewContentActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("uri", uri);
        activity.startActivity(intent);

    }
}
